import 'package:base/main.dart';
import 'package:framework/core/base_view_model.dart';
import 'package:module_discover/enums/domain_type_enum.dart';
import 'package:module_discover/pages/model/Domain_info_model.dart';
import 'package:module_discover/utils/dis_cons.dart';

/// 筛选预览VM
class FilterPreviewVM extends BaseViewModel {
  /// 回显数据集合
  final selectedMaps = <String, Map<String, DomainInfoModel>>{}.obs;

  /// 设置选中数据map封装
  /// 单选和多选的处理
  setSelectMaps(String parentKey, bool? sChoice, bool? isSelected,
      String childKey, String name, String key, String? value) {
    final childMap = selectedMaps.putIfAbsent(
      parentKey,
      () => <String, DomainInfoModel>{},
    );
    DomainInfoModel createModel(String value) => DomainInfoModel(
        name: name,
        parentKey: parentKey,
        childKey: childKey,
        key: key,
        value: value);

    /// 单选
    if (sChoice == true) {
      if (value != null) {
        childMap[childKey] = createModel(value);
      }
    }

    /// 多选
    else {
      final existing = childMap[childKey];
      if (existing == null) {
        if (isSelected == true) {
          childMap[childKey] = createModel(value!.trim());
        }
      } else {
        final trimmedKey = value!.trim();
        final valuesList = existing.value
            .split(DisCons.SPLIT_S)
            .map((v) => v.trim())
            .where((v) => v.isNotEmpty)
            .toList();
        if (isSelected == true) {
          if (!valuesList.contains(trimmedKey)) {
            valuesList.add(trimmedKey);
          }
        } else {
          valuesList.remove(trimmedKey);
        }
        if (valuesList.isEmpty) {
          childMap.remove(childKey);
        } else {
          final newValue = valuesList.join(DisCons.SPLIT_S);
          childMap[childKey] = existing.copyWith(value: newValue);
        }
      }
    }
    selectedMaps.forEach((parentKey, childMap) {
      childMap.forEach((key, info) {
        Log.d(
            "FilterPreviewVM data =  [$parentKey-$childKey]; name=${info.name},key=${info.key}, value=${info.value}");
      });
    });
    selectedMaps.refresh();
  }

  /// 设置选中数据map封装
  /// 输入框和自定义输入框选择框的处理
  void setSelectedMaps(DomainTypeEnum domainType, String name,
      String? parentKey, String? childKey, String key, String keyValue,
      {bool? isShow = true}) {
    if (parentKey == null || childKey == null) return;
    final childMap = selectedMaps.putIfAbsent(
      parentKey,
      () => <String, DomainInfoModel>{},
    );
    DomainInfoModel createModel(String value) => DomainInfoModel(
          name: name,
          parentKey: parentKey,
          childKey: childKey,
          key: key,
          value: value,
          isShow: isShow,
        );
    if (domainType == DomainTypeEnum.SELECT2) {
      childMap[childKey] = createModel(keyValue);
    } else if (domainType == DomainTypeEnum.INPUT) {
      if (keyValue == "") {
        childMap.remove(childKey);
      } else {
        childMap[childKey] = createModel(keyValue);
      }
    } else if (domainType == DomainTypeEnum.INPUT2) {
      childMap[childKey] = createModel(keyValue);
    }
    selectedMaps.forEach((parentKey, childMap) {
      childMap.forEach((key, info) {
        Log.d(
            "FilterPreviewVM data =  [$parentKey-$childKey]; name=${info.name},key=${info.key}, value=${info.value}");
      });
    });
    selectedMaps.refresh();
  }

  /// 删除选中的数据集合
  void deleteSelectedMap(String parentKey, String childKey) {
    selectedMaps[parentKey]?.remove(childKey);
    selectedMaps.refresh();
  }

  /// 获取已选数据集合的条件个数
  int getSelectedChildSize() {
    var length = 0;
    selectedMaps.forEach((parentKey, childMap) {
      childMap.forEach((key, info) {
        if (info.isShow == true) {
          length++;
        }
      });
    });
    return length;
  }

  /// 根据key获取已选数据
  String getSelectedValue(String? parentKey, String? childKey) {
    if (parentKey == null || childKey == null) return "";
    final currentValue = selectedMaps[parentKey]?[childKey]?.value;
    if (currentValue == null) return "";
    return currentValue;
  }

  /// 根据key获取已选数据条件个数
  int getSelectedChildSizeByParentKey(String parentKey) {
    var length = 0;
    selectedMaps[parentKey]?.forEach((key, info) {
      if (info.isShow == true) {
        length++;
      }
    });
    return length;
  }

  /// 获取预览列表
  List<DomainInfoModel> getSelectChildLists() {
    List<DomainInfoModel> model = [];
    selectedMaps.forEach((parentKey, childMap) {
      childMap.forEach((key, info) {
        if (info.isShow == true) {
          model.add(info);
        }
      });
    });
    return model;
  }

  /// 重置条件
  void resettingMap() {
    selectedMaps.value = selectedMaps.value.map(
      (parentKey, childMap) => MapEntry(parentKey, {}),
    );
    selectedMaps.refresh();
  }
}
